package com.osig.ec.cfca;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import cfca.paperless.client.bean.FieldBean;
import cfca.paperless.client.bean.ImageBean;
import cfca.paperless.client.bean.TextBean;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;

public class testSynthesizeAutoTemplate {

	static String templateCode = "dg002 ";

	public static void main(String[] args) throws Exception {

		PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url);

		String bizXmlId = GUID.generateId();
		System.out.println(bizXmlId);
		String fieldBeanListXml = PaperlessUtil.buildFieldBeanListXml(getFieldBeanList(), bizXmlId, "10");
		System.out.println(fieldBeanListXml);
		String textBeanListXml = PaperlessUtil.buildTextBeanListXml(getTextBeanList(), GUID.generateId(), "10000");
		String imageBeanListXml = PaperlessUtil.buildImageBeanListXml(getImageBeanList());

		String savedPdfId = GUID.generateId();
		System.out.println(savedPdfId);
		byte[] pdf = clientBean.synthesizeAutoTemplate(templateCode, savedPdfId, fieldBeanListXml, textBeanListXml,
				imageBeanListXml, PaperlessConfig.operatorCode, PaperlessConfig.channelCode);
		String errorRsString = PaperlessUtil.isError(pdf);
		if ("".equals(errorRsString)) {
			IoUtil.write("D://testcfca//output/synthesizeAutoTemplate.pdf", pdf);
		} else {
			System.out.println(new String(pdf, "UTF-8"));
		}

	}

	private static List<FieldBean> getFieldBeanList() throws Exception {
		List<FieldBean> fieldBeanList = new ArrayList<FieldBean>();
		String address = "11的快捷付款捷付款的机房空间的开发及啊啊啊啊啊啊啊啊啊啊1111111111111的快捷付款的机房空间的开发及啊啊啊啊啊啊啊啊啊啊";
		fieldBeanList.add(new FieldBean("year", "2016", "", "text"));
		fieldBeanList.add(new FieldBean("date", "2011年11月11", "", "text", 2));
		fieldBeanList.add(new FieldBean("branch", "ddddddddddddd", "", "text", 2));
		fieldBeanList.add(new FieldBean("fingerCode", "Sucaili", "", "text", 2));
		fieldBeanList.add(new FieldBean("signature1", "Sucaili", "", "text", 1));
		fieldBeanList.add(new FieldBean("identNo", "Sucaili", "", "text", 1));
		fieldBeanList.add(new FieldBean("identType", "Sucaili", "", "text"));
		fieldBeanList.add(new FieldBean("ICcardType", "Sucaili", "", "text", 1));
		fieldBeanList.add(new FieldBean("accountName", "张凡的", "", "text", 1));
		fieldBeanList.add(new FieldBean("cardNo1", "Sucaili", "", "text", 1));
		fieldBeanList.add(new FieldBean("sequenceNo1", "Sucaili", "", "text", 1));
		fieldBeanList.add(new FieldBean("address", address, "", "text", 3));
		fieldBeanList.add(new FieldBean("text1", "Sucaili", "", "text", 1));
		fieldBeanList.add(new FieldBean("toggle_25", "On", "", "checkbox"));
		fieldBeanList.add(new FieldBean("checkbox1", "cfca", "", "checkbox"));

		return fieldBeanList;
	}

	private static List<TextBean> getTextBeanList() throws Exception {
		List<TextBean> textBeanList = new ArrayList<TextBean>();
		TextBean textBean1 = new TextBean("你好啊", "", "银行签章：", "R", 0, 0);
		textBean1.setTextFontSize("15");
		textBean1.setTextColor("aa000000");
		TextBean textBean2 = new TextBean("545454545452212323", "", "单据号：", "R", 0, 0, 2);
		textBean2.setTextFontSize("15");
		textBean2.setTextColor("aa000000");
		textBeanList.add(textBean1);
		textBeanList.add(textBean2);

		return textBeanList;
	}

	private static List<ImageBean> getImageBeanList() throws Exception {
		List<ImageBean> imageBeanList = new ArrayList<ImageBean>();
		// 如下代码是在pdf中添加可以显示的图片
		String imageData = Base64.toBase64String(IoUtil.read(new FileInputStream("D://testcfca//1.png")));
		ImageBean imageBean0 = new ImageBean("100", "100", "签章", "C", 0, 0, imageData, 1);
		ImageBean imageBean = new ImageBean("100", "100", 1, "400", "300", imageData, 2);
		imageBeanList.add(imageBean0);
		imageBeanList.add(imageBean);
		return imageBeanList;
	}
}
